package org.budo.mybatis.dao.statement.builder.creator;

import org.apache.ibatis.session.Configuration;
import org.budo.mybatis.dao.statement.builder.impl.DefaultStatementBuilder;

/**
 * @author lmw
 */
public class ListByIdsStatementCreator extends AbstractStatementCreator {
    public ListByIdsStatementCreator(Configuration configuration, DefaultStatementBuilder statementBuilder) {
        super(configuration, statementBuilder);
    }

    protected String getStatementId() {
        return STATEMENT_LIST_BY_IDS;
    }

    protected String createStatement(Class<?> entityType) {
        String tableName = this.getStatementBuilder().getEntityConfiger().getTableName(entityType);
        String idColumn = this.getStatementBuilder().getEntityConfiger().getIdColumn(entityType);

        String resultMapOrResultType = this.getStatementBuilder().resultMapOrResultType(this.getConfiguration(), entityType, null);

        String statementXml = _4 + "<select id=\"" + this.getStatementId() + "\" " + resultMapOrResultType + ">\n"//
                + _8 + "SELECT * FROM " + tableName + " WHERE " + idColumn + " IN \n" //
                + _8 + "<foreach item=\"item\" index=\"index\" collection=\"array\" separator=\",\" open=\"(\" close=\")\">\n" //
                + _12 + "#{item}\n" //
                + _8 + "</foreach> " + "\n" //
                + _4 + "</select>"; //

        return statementXml;
    }
}